/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jmail;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author iflamed
 */
public final class Config extends Properties{

    private static Properties props = new Properties();
    private final static String FILEPATH = "jmail.properties";
    
    public static void addProp(String key, String value) {
        if (! props.containsKey(key)) {
            props.put(key, value);
        } else {
            props.setProperty(key, value);
        }
    }

    public static boolean initialProps() {
        FileInputStream in;
        try {
            File f = new File(FILEPATH);
            if (f.exists()) {
                in = new FileInputStream(FILEPATH);
                props.load(in);
                in.close();
                return true;
            } else {
                dumpProps();
                return true;
            }
        } catch (IOException ex) {
            Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
            dumpProps();
            return false;
        }
    }

    public static boolean dumpProps() {
        try {
            if (!props.containsKey("jmail.dir")) {
                props.put("jmail.dir", "./");
            }
            if(!props.containsKey("jmail.author")){
                props.put("jmail.author", "iflamed@126.com");
            }
            FileOutputStream out = new FileOutputStream(FILEPATH);
            props.store(out, "Jmail config file,Do not delete!");           
            out.close();
            return true;
        } catch (IOException ex) {
            Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }
    
    public  static String getProps(String key){
        return props.getProperty(key);
    }
}
